using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace Microsoft.Expression.Controls
{
	[StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(PathListBoxItem))]
	public sealed class PathListBox : ListBox
	{
		private PathPanel itemsPanel;
		private static readonly DependencyProperty ItemsPanelListenerProperty = DependencyProperty.Register("ItemsPanelListener", typeof(ItemsPanelTemplate), typeof(PathListBox), new PropertyMetadata(new PropertyChangedCallback(PathListBox.ItemsPanelChanged)));
		public static readonly DependencyProperty LayoutPathsProperty = DependencyProperty.Register("LayoutPaths", typeof(LayoutPathCollection), typeof(PathListBox), new PropertyMetadata(null));
		public static readonly DependencyProperty StartItemIndexProperty = DependencyProperty.Register("StartItemIndex", typeof(double), typeof(PathListBox), new PropertyMetadata(0.0));
		public static readonly DependencyProperty WrapItemsProperty = DependencyProperty.Register("WrapItems", typeof(bool), typeof(PathListBox), new PropertyMetadata(false));
		private ItemsPanelTemplate ItemsPanelListener
		{
			get
			{
				return (ItemsPanelTemplate)base.GetValue(PathListBox.ItemsPanelListenerProperty);
			}
			set
			{
				base.SetValue(PathListBox.ItemsPanelListenerProperty, value);
			}
		}
		public LayoutPathCollection LayoutPaths
		{
			get
			{
				return (LayoutPathCollection)base.GetValue(PathListBox.LayoutPathsProperty);
			}
		}
		public double StartItemIndex
		{
			get
			{
				return (double)base.GetValue(PathListBox.StartItemIndexProperty);
			}
			set
			{
				base.SetValue(PathListBox.StartItemIndexProperty, value);
			}
		}
		public bool WrapItems
		{
			get
			{
				return (bool)base.GetValue(PathListBox.WrapItemsProperty);
			}
			set
			{
				base.SetValue(PathListBox.WrapItemsProperty, value);
			}
		}
		public PathListBox()
		{
			base.set_DefaultStyleKey(typeof(PathListBox));
			LayoutPathCollection layoutPathCollection = new LayoutPathCollection();
			base.SetValue(PathListBox.LayoutPathsProperty, layoutPathCollection);
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.itemsPanel = null;
		}
		protected override DependencyObject GetContainerForItemOverride()
		{
			return new PathListBoxItem();
		}
		protected override bool IsItemItsOwnContainerOverride(object item)
		{
			return item is PathListBoxItem;
		}
		protected override Size ArrangeOverride(Size finalSize)
		{
			if (this.ItemsPanelListener == null)
			{
				DependencyProperty arg_28_1 = PathListBox.ItemsPanelListenerProperty;
				Binding binding = new Binding("ItemsPanel");
				binding.set_Source(this);
				binding.set_Mode(1);
				base.SetBinding(arg_28_1, binding);
			}
			if (this.itemsPanel == null)
			{
				this.itemsPanel = (this.GetItemsHost() as PathPanel);
				if (this.itemsPanel != null)
				{
					this.RefreshSourceElementBindings();
					this.SetOneWayBinding(this.itemsPanel, PathPanel.LayoutPathsProperty, "LayoutPaths");
					this.SetOneWayBinding(this.itemsPanel, PathPanel.StartItemIndexProperty, "StartItemIndex");
					this.SetOneWayBinding(this.itemsPanel, PathPanel.WrapItemsProperty, "WrapItems");
				}
			}
			return base.ArrangeOverride(finalSize);
		}
		private void RefreshSourceElementBindings()
		{
			using (IEnumerator<LayoutPath> enumerator = this.LayoutPaths.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					LayoutPath current = enumerator.get_Current();
					if (current.SourceElement == null)
					{
						BindingExpression bindingExpression = current.ReadLocalValue(LayoutPath.SourceElementProperty) as BindingExpression;
						if (bindingExpression != null)
						{
							current.ClearValue(LayoutPath.SourceElementProperty);
							current.SetValue(LayoutPath.SourceElementProperty, bindingExpression);
						}
					}
				}
			}
		}
		private static void ItemsPanelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			PathListBox pathListBox = d as PathListBox;
			if (pathListBox == null)
			{
				return;
			}
			if (e.get_Property() == PathListBox.ItemsPanelListenerProperty && e.get_NewValue() != e.get_OldValue())
			{
				pathListBox.itemsPanel = null;
			}
			pathListBox.InvalidateArrange();
		}
		private void SetOneWayBinding(DependencyObject target, DependencyProperty targetProperty, string sourceProperty)
		{
			Binding binding = new Binding(sourceProperty);
			binding.set_Source(this);
			binding.set_Mode(1);
			BindingOperations.SetBinding(target, targetProperty, binding);
		}
	}
}
